package com.l024.easyandroidlib.log;

import androidx.annotation.NonNull;

import com.l024.easyandroidlib.log.config.EasyLogConfig;
import com.l024.easyandroidlib.log.log.EasyLogManager;
import com.l024.easyandroidlib.log.log.EasyLogType;
import com.l024.easyandroidlib.log.printer.EasyLogPrinter;
import com.l024.easyandroidlib.log.util.EasyLogStackTraceUtil;

import java.util.Arrays;
import java.util.List;

/**
 * @author wu_ming_zhi_bei
 * @date 2021/1/28 21:30
 * @Notes 1.打印堆信息
 *  2.File输出
 *  3.模拟控制台
 */
public class EasyLog {

    private static final String EASY_LOG_PACKAGE;
    static {
        String className = EasyLog.class.getName();
        EASY_LOG_PACKAGE = className.substring(0,className.lastIndexOf("."));
    }

    public static void v(Object ...contents){
        log(EasyLogType.V,contents);
    }
    public static void vt(String tag,Object ...contents){
        log(EasyLogType.V,tag,contents);
    }

    public static void d(Object ...contents){
        log(EasyLogType.D,contents);
    }
    public static void vd(String tag,Object ...contents){
        log(EasyLogType.D,tag,contents);
    }

    public static void i(Object ...contents){
        log(EasyLogType.I,contents);
    }
    public static void vi(String tag,Object ...contents){
        log(EasyLogType.I,tag,contents);
    }

    public static void w(Object ...contents){
        log(EasyLogType.W,contents);
    }
    public static void vw(String tag,Object ...contents){
        log(EasyLogType.W,tag,contents);
    }

    public static void e(Object ...contents){
        log(EasyLogType.E,contents);
    }
    public static void ve(String tag,Object ...contents){
        log(EasyLogType.E,tag,contents);
    }

    public static void a(Object ...contents){
        log(EasyLogType.A,contents);
    }
    public static void va(String tag,Object ...contents){
        log(EasyLogType.A,tag,contents);
    }

    public static void log(@EasyLogType.TYPE int type,Object ...contents){
        log(type, EasyLogManager.getInstance().getConfig().getGlobalTag(),contents);
    }

    public static void log(@EasyLogType.TYPE int type, @NonNull String tag, Object ...contents){
        log(EasyLogManager.getInstance().getConfig(),type,tag,contents);
    }

    public static void log(@NonNull EasyLogConfig config, @EasyLogType.TYPE int type, @NonNull String tag, Object ...contents){
        if(!config.enable()){
            return;
        }
        StringBuilder sb = new StringBuilder();
        //判断是否要包含线程信息
        if(config.includeThread()){
            String threadInfo = EasyLogConfig.EASY_LOG_THREAD_FORMATTER.format(Thread.currentThread());
            sb.append(threadInfo).append("\n");
        }
        //判断是否要打印堆栈信息
        if(config.stackTraceDepth()>0){
            String stackTrace = EasyLogConfig.EASY_LOG_STACK_TRACE_FORMATTER
                    .format(EasyLogStackTraceUtil.getCroppedRealStackTrace(new Throwable().getStackTrace(),EASY_LOG_PACKAGE ,config.stackTraceDepth()));
            sb.append(stackTrace).append("\n");
        }
        String body = parseBody(contents,config);
        sb.append(body);
        //Log.println(type,tag,body);
        //判断我们自己的打印器是否存在
        List<EasyLogPrinter> printers = config.printers()!=null? Arrays.asList(config.printers()):EasyLogManager.getInstance().getPrinters();
        if(printers==null){
            return;
        }
        //打印
        for(EasyLogPrinter printer:printers){
            printer.print(config,type,tag,sb.toString());
        }
    }

    /**
     * 数据转字符
     * @return
     */
    private static String parseBody(@NonNull Object[] contents,@NonNull EasyLogConfig config){
        if(config.injectJsonParser()!=null){
            return config.injectJsonParser().toJson(contents);
        }
        StringBuilder sb = new StringBuilder();
        for (Object o:contents){
            sb.append(o.toString()).append(";");
        }
        if(sb.length()>0){
            sb.deleteCharAt(sb.length()-1);
        }
        return sb.toString();
    }
}
